package plugproxy;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.*;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.peer.PeerFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.Map;

/**
 * plugproxy project component
 *
 * @author Jacky Chan (linux_china@hotmail.com)
 */
public class PlugProxyProjectComponent implements ProjectComponent, JDOMExternalizable {
    public static final String TOOL_WINDOW_ID = "PlugProxy";
    private Project project;
    private static final Icon icon = IconLoader.getIcon("/plugproxy/icons/monitor.png");
    private PlugProxyGui panel = new PlugProxyGui();
    public String host;
    public String hostPort;
    public String localPort;
    public String hexDump;

    /**
     * construction method for project component
     *
     * @param project project object
     */
    public PlugProxyProjectComponent(Project project) {
        this.project = project;
    }

    /**
     * fired before component initialization
     */
    public void initComponent() {

    }

    /**
     * fired after component disposed
     */
    public void disposeComponent() {

    }

    /**
     * get component name
     *
     * @return component name
     */
    @NotNull
    public String getComponentName() {
        return "PlugProxyProjectComponent";
    }

    /**
     * save settings
     *
     * @param element destination element
     * @throws WriteExternalException exception
     */
    public void writeExternal(Element element) throws WriteExternalException {
        Map<String, String> info = panel.getSettings();
        host = info.get("host");
        hostPort = info.get("hostPort");
        localPort = info.get("localPort");
        hexDump = info.get("hextDump");
        DefaultJDOMExternalizer.writeExternal(this, element);
    }

    /**
     * read settings
     *
     * @param element desination element
     * @throws InvalidDataException exception
     */
    public void readExternal(Element element) throws InvalidDataException {
        DefaultJDOMExternalizer.readExternal(this, element);
        panel.setSettings(host, hostPort, localPort, hexDump);
    }

    /**
     * fired after project opened
     */
    public void projectOpened() {
        registerPlugProxyToolWindow(project);
    }

    /**
     * fired after project closed
     */
    public void projectClosed() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        toolWindowManager.unregisterToolWindow(TOOL_WINDOW_ID);
    }

    /**
     * register plug proxy tool window
     *
     * @param project project object
     */
    public void registerPlugProxyToolWindow(Project project) {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        JFrame jFrame = WindowManager.getInstance().getFrame(project);
        ToolWindow toolWindow = toolWindowManager.registerToolWindow(TOOL_WINDOW_ID, false, ToolWindowAnchor.BOTTOM);
        ContentFactory contentFactory = PeerFactory.getInstance().getContentFactory();
        Content content = contentFactory.createContent(panel, "", false);
        toolWindow.getContentManager().addContent(content);
        toolWindow.setIcon(icon);
    }
}
